﻿using Sidvall.Serialization;
using System.Linq;

namespace Sidvall.WorkItemManager.Data.Model.EntityManagers
{
    public sealed partial class WorkItemTypeManager<TEntity, TList> : global::Sidvall.Data.EntityManagerBase
        where TEntity : global::Sidvall.WorkItemManager.Data.IWorkItemType
        where TList : Sidvall.Data.IDataRowProvider<IWorkItemType>
    {
        #region Public Members

        #region AcceptChanges

        public void AcceptChanges(global::Sidvall.WorkItemManager.Data.IWorkItemType value)
        {
            AcceptChanges(value, null);
        }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")]
        public void AcceptChanges(global::Sidvall.WorkItemManager.Data.IWorkItemType value, Sidvall.Data.AcceptChangesSettings acceptChangesSettings)
        {
            if (value == null)
                return;
            if (value.DataRowStateIsDirty)
            {
                if (acceptChangesSettings == null)
                    acceptChangesSettings = new Sidvall.Data.AcceptChangesSettings();
                Sidvall.Data.EntityManager.AcceptItemChanges(value, acceptChangesSettings);
                this.EntityFacadeManager.GenericEntityFacade.WorkItemManager.AcceptChanges(value.WorkItems, acceptChangesSettings);
            }
        }
        public void AcceptChanges(System.Collections.Generic.IEnumerable<global::Sidvall.WorkItemManager.Data.IWorkItemType> value)
        {
            AcceptItemsChanges(value, null);
        }
        public void AcceptChanges(System.Collections.Generic.IEnumerable<global::Sidvall.WorkItemManager.Data.IWorkItemType> value, Sidvall.Data.AcceptChangesSettings acceptChangesSettings)
        {
            AcceptItemsChanges(value, acceptChangesSettings);
        }

        #endregion
        #region SetBusinessSettings

        public void SetBusinessSettings(global::Sidvall.WorkItemManager.Data.IWorkItemType value, global::Sidvall.Business.BusinessSettings businessSettings, global::Sidvall.Business.BusinessCollectionSettings businessCollectionSettings)
        {
            if (value == null)
                return;
            var provider = value as global::Sidvall.Business.IBusinessSettingsProvider;
            if (provider != null)
                provider.BusinessSettings.Import(businessSettings);
            this.EntityFacadeManager.GenericEntityFacade.WorkItemManager.SetBusinessSettings(value.WorkItems, businessSettings, businessCollectionSettings);
        }
        public void SetBusinessSettings(System.Collections.Generic.IEnumerable<global::Sidvall.WorkItemManager.Data.IWorkItemType> value, global::Sidvall.Business.BusinessSettings businessSettings, global::Sidvall.Business.BusinessCollectionSettings businessCollectionSettings)
        {
            if (value == null)
                return;
            var provider = value as global::Sidvall.Business.IBusinessCollectionSettingsProvider;
            if (provider != null)
                provider.BusinessSettings.Import(businessCollectionSettings);
            foreach (var item in value)
                SetBusinessSettings(item, businessSettings, businessCollectionSettings);
        }

        #endregion
        #region GetDataRowStateCounts

        public global::Sidvall.Data.DataRowStateCountItemCollection GetDataRowStateCounts(global::Sidvall.WorkItemManager.Data.IWorkItemType value)
        {
            var items = new global::Sidvall.Data.DataRowStateCountItemCollection();
            LoadDataRowStateCounts(items, value);
            return items;
        }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")]
        internal void LoadDataRowStateCounts(global::Sidvall.Data.DataRowStateCountItemCollection items, global::Sidvall.WorkItemManager.Data.IWorkItemType value)
        {
            if (value == null)
                return;
            items.Add(global::Sidvall.WorkItemManager.Data.Model.DomainObjects.WorkItemType, value);
            this.EntityFacadeManager.GenericEntityFacade.WorkItemManager.LoadDataRowStateCounts(items, value.WorkItems);
        }
        public global::Sidvall.Data.DataRowStateCountItemCollection GetDataRowStateCounts(System.Collections.Generic.IEnumerable<global::Sidvall.WorkItemManager.Data.IWorkItemType> value)
        {
            var items = new global::Sidvall.Data.DataRowStateCountItemCollection();
            LoadDataRowStateCounts(items, value);
            return items;
        }
        internal void LoadDataRowStateCounts(global::Sidvall.Data.DataRowStateCountItemCollection items, System.Collections.Generic.IEnumerable<global::Sidvall.WorkItemManager.Data.IWorkItemType> value)
        {
            if (value == null)
                return;
            items.Add(global::Sidvall.WorkItemManager.Data.Model.DomainObjects.WorkItemType, value as Sidvall.Data.IListContextProvider);
            foreach (var item in value)
                LoadDataRowStateCounts(items, item);
        }

        #endregion
        #region LoadInstanceField

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        public void LoadInstanceField(System.Collections.Generic.IEnumerable<global::Sidvall.WorkItemManager.Data.IWorkItemType> items, string fieldName, object value, bool cascade, bool raisePropertyChanged, bool updateState)
        {
            if (items == null)
                return;
            foreach (var item in items)
                LoadInstanceField(item, fieldName, value, cascade, raisePropertyChanged, updateState);
        }
        partial void OnAfterLoadInstanceField(string fieldName, object value, bool cascade, bool raisePropertyChanged, bool updateState);
        partial void OnBeforeLoadInstanceField(string fieldName, object value, bool cascade, bool raisePropertyChanged, bool updateState);
        partial void OnLoadInstanceField(string fieldName, object value, bool cascade, bool raisePropertyChanged, bool updateState);
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "cascade"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1505:AvoidUnmaintainableCode")]
        public void LoadInstanceField(global::Sidvall.WorkItemManager.Data.IWorkItemType item, string fieldName, object value, bool cascade, bool raisePropertyChanged, bool updateState)
        {
            if (item == null)
                return;
            if (fieldName == null)
                return;
            OnBeforeLoadInstanceField(fieldName, value, cascade, raisePropertyChanged, updateState);
            switch (fieldName)
            {
                case "Codename":
                    value = Sidvall.Business.ValidationManager.GetValidString(value, false, 2000, true);
                    item.Codename = (string)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "Codename", raisePropertyChanged, updateState);
                    break;
                case "ContextEditor":
                    value = Sidvall.Business.ValidationManager.GetValidString(value, false, 2000, true);
                    item.ContextEditor = (string)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "ContextEditor", raisePropertyChanged, updateState);
                    break;
                case "Description":
                    value = Sidvall.Business.ValidationManager.GetValidString(value, false, null, true);
                    item.Description = (string)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "Description", raisePropertyChanged, updateState);
                    break;
                case "ExecutionContextEditor":
                    value = Sidvall.Business.ValidationManager.GetValidString(value, false, 2000, true);
                    item.ExecutionContextEditor = (string)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "ExecutionContextEditor", raisePropertyChanged, updateState);
                    break;
                case "Name":
                    value = Sidvall.Business.ValidationManager.GetValidString(value, false, 255, true);
                    item.Name = (string)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "Name", raisePropertyChanged, updateState);
                    break;
                case "WorkItemTypeId":
                    value = Sidvall.Business.ValidationManager.GetValidInt64(value, null, null);
                    item.WorkItemTypeId = (System.Nullable<long>)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "WorkItemTypeId", raisePropertyChanged, updateState);
                    if (cascade)
                    {
                        this.EntityFacadeManager.GenericEntityFacade.WorkItemManager.LoadInstanceField(item.WorkItems, "WorkItemTypeId", value, cascade, raisePropertyChanged, updateState);
                    }
                    break;
                case "ServerConnectionId":
                    value = Sidvall.Business.ValidationManager.GetValidInt32(value, null, null);
                    item.ServerConnectionId = (System.Nullable<System.Int32>)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "ServerConnectionId", raisePropertyChanged, updateState);
                    break;
                case "WorkItems":
                    item.SetWorkItems((Sidvall.Data.IDataRowProvider<global::Sidvall.WorkItemManager.Data.IWorkItem>)value);
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "WorkItems", raisePropertyChanged, updateState);
                    if (cascade)
                    {
                        if (item.WorkItems != null)
                        {
                            this.EntityFacadeManager.GenericEntityFacade.WorkItemManager.LoadInstanceField(item.WorkItems, "WorkItemTypeId", item.WorkItemTypeId, cascade, raisePropertyChanged, updateState);
                        }
                    }
                    break;
                default:
                    OnLoadInstanceField(fieldName, value, cascade, raisePropertyChanged, updateState);
                    break;
            }
            OnAfterLoadInstanceField(fieldName, value, cascade, raisePropertyChanged, updateState);
        }

        #endregion
        #region Clone

        public TEntity Clone(IWorkItemType value)
        {
            return Copy(value, Sidvall.Data.CopySettings.Clone());
        }
        public TList Clone(System.Collections.Generic.IEnumerable<IWorkItemType> value)
        {
            return Copy(value, Sidvall.Data.CopySettings.Clone());
        }

        #endregion
        #region Copy

        public TEntity Copy(IWorkItemType value)
        {
            return Copy(value, null, string.Empty, string.Empty);
        }
        public TEntity Copy(IWorkItemType value, Sidvall.Data.CopySettings copySettings)
        {
            return Copy(value, copySettings, string.Empty, string.Empty);
        }
        internal TEntity Copy(IWorkItemType value, Sidvall.Data.CopySettings copySettings, string parent, string child)
        {
            if (copySettings == null)
                copySettings = new Sidvall.Data.CopySettings();
            if (!Sidvall.Data.DataRowContextManager.IsValid(value, copySettings.RowFilter))
                return default(TEntity);
            var item = (TEntity)this.EntityFacadeManager.ResourceFactory.CreateItem(global::Sidvall.WorkItemManager.Data.Model.DomainObjects.WorkItemType, null);
            if (item == null)
                return default(TEntity);
            Import(item, value, copySettings, parent, child);
            return item;
        }

        public TList Copy(System.Collections.Generic.IEnumerable<IWorkItemType> value)
        {
            return Copy(value, null, string.Empty, string.Empty);
        }
        public TList Copy(System.Collections.Generic.IEnumerable<IWorkItemType> value, Sidvall.Data.CopySettings copySettings)
        {
            return Copy(value, copySettings, string.Empty, string.Empty);
        }
        internal TList Copy(System.Collections.Generic.IEnumerable<IWorkItemType> value, Sidvall.Data.CopySettings copySettings, string parent, string child)
        {
            var items = (TList)this.EntityFacadeManager.ResourceFactory.CreateItems(global::Sidvall.WorkItemManager.Data.Model.DomainObjects.WorkItemType);
            Import(items, value, copySettings, parent, child);
            return items;
        }

        #endregion
        #region GetKey

        public string GetKey(IWorkItemType item)
        {
            if (item == null)
                return null;
            if (item.WorkItemTypeId == null)
                return null;
            return item.WorkItemTypeId.ToString();
        }
        public string GetKey(long workItemTypeId)
        {
            return workItemTypeId.ToString(System.Globalization.CultureInfo.InvariantCulture);
        }
        public string GetKey(Sidvall.Data.Filter<global::Sidvall.WorkItemManager.Data.Fields.WorkItemTypeField> item)
        {
            if (item == null)
                return null;
            var part = item.FilterItems.GetFilterItemValue(Sidvall.WorkItemManager.Data.Fields.WorkItemTypeField.WorkItemTypeId, null);
            if (part == null)
                return null;
            var value = part;
            return value;
        }

        #endregion
        #region ParseKey

        public System.Nullable<long> ParseKey(string key)
        {
            return key.ToNullableInt64();
        }

        #endregion
        #region Import

        public void Import(TEntity destination, IWorkItemType source)
        {
            Import(destination, source, null, string.Empty, string.Empty);
        }
        public void Import(TEntity destination, IWorkItemType source, Sidvall.Data.CopySettings copySettings)
        {
            Import(destination, source, copySettings, string.Empty, string.Empty);
        }
        partial void OnAfterImport(TEntity destination, IWorkItemType source, Sidvall.Data.CopySettings copySettings, string parent, string child);
        partial void OnBeforeImport(TEntity destination, IWorkItemType source, Sidvall.Data.CopySettings copySettings, ref string parent, ref string child);
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "parent"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "child"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]
        internal void Import(TEntity destination, IWorkItemType source, Sidvall.Data.CopySettings copySettings, string parent, string child)
        {
            if (destination == null)
                return;
            if (source == null)
                return;
            if (copySettings == null)
                copySettings = new Sidvall.Data.CopySettings();
            OnBeforeImport(destination, source, copySettings, ref parent, ref child);
            var accessLevelProvider = source as Sidvall.Security.IPropertyAccessLevelProvider;
            if ((copySettings.FieldCopyOption & Sidvall.Business.FieldCopyOptions.AutomaticPrimaryKeys) == Sidvall.Business.FieldCopyOptions.AutomaticPrimaryKeys)
            {
                if (copySettings.IncludedProperty(accessLevelProvider, true, true, false, Sidvall.Data.DataType.Int64, "WorkItemType.WorkItemTypeId", "WorkItemType"))
                    destination.WorkItemTypeId = source.WorkItemTypeId;
            }
            if ((copySettings.FieldCopyOption & Sidvall.Business.FieldCopyOptions.ManualNonPrimaryKeys) == Sidvall.Business.FieldCopyOptions.ManualNonPrimaryKeys)
            {
                if (copySettings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.String, "WorkItemType.Codename", "WorkItemType"))
                    destination.Codename = source.Codename;
                if (copySettings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.String, "WorkItemType.ContextEditor", "WorkItemType"))
                    destination.ContextEditor = source.ContextEditor;
                if (copySettings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.String, "WorkItemType.Description", "WorkItemType"))
                    destination.Description = source.Description;
                if (copySettings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.String, "WorkItemType.ExecutionContextEditor", "WorkItemType"))
                    destination.ExecutionContextEditor = source.ExecutionContextEditor;
                if (copySettings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.String, "WorkItemType.Name", "WorkItemType"))
                    destination.Name = source.Name;
                if (copySettings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Int32, "WorkItemType.ServerConnectionId", "WorkItemType"))
                    destination.ServerConnectionId = source.ServerConnectionId;
            }
            if (copySettings.IncludeChildren)
            {
                if (child != "WorkItem")
                {
                    if (copySettings.IncludedProperty(accessLevelProvider, false, false, true, Sidvall.Data.DataType.Object, "WorkItemType.WorkItems", "WorkItem"))
                    {
                        if (destination.WorkItems != null)
                            this.EntityFacadeManager.GenericEntityFacade.WorkItemManager.Import(destination.WorkItems, source.WorkItems, copySettings, "WorkItemType", string.Empty);
                        else if (source.WorkItems != null)
                            destination.SetWorkItems(this.EntityFacadeManager.GenericEntityFacade.WorkItemManager.Copy(source.WorkItems, copySettings, "WorkItemType", string.Empty));
                    }
                }
            }
            Sidvall.Data.EntityManager.AfterImportItem(destination, source, copySettings);
            OnAfterImport(destination, source, copySettings, parent, child);
        }

        public void Import(System.Collections.Generic.IEnumerable<IWorkItemType> destination, System.Collections.Generic.IEnumerable<IWorkItemType> source)
        {
            Import(destination, source, null, string.Empty, string.Empty);
        }
        public void Import(System.Collections.Generic.IEnumerable<IWorkItemType> destination, System.Collections.Generic.IEnumerable<IWorkItemType> source, Sidvall.Data.CopySettings copySettings)
        {
            Import(destination, source, copySettings, string.Empty, string.Empty);
        }
        public void Import(System.Collections.Generic.IEnumerable<IWorkItemType> destination, System.Collections.Generic.IEnumerable<IWorkItemType> source, long? totalRowCount, Sidvall.Data.CopySettings copySettings)
        {
            Sidvall.Data.ITotalRowCount rowCountProvider;

            if (source == null)
                return;
            Import(destination, source, copySettings, string.Empty, string.Empty);
            if (totalRowCount != null)
            {
                rowCountProvider = destination as Sidvall.Data.ITotalRowCount;
                if (rowCountProvider != null)
                    rowCountProvider.TotalRowCount = totalRowCount.Value;
            }
        }
        internal void Import(Sidvall.Data.IDataRowProvider<IWorkItemType> destination, System.Collections.Generic.IEnumerable<IWorkItemType> source, Sidvall.Data.CopySettings copySettings, string parent, string child)
        {
            Import(destination.DataItems, source, copySettings, parent, child);
        }
        partial void OnBeforeImport(System.Collections.Generic.IEnumerable<IWorkItemType> destination, System.Collections.Generic.IEnumerable<IWorkItemType> source, Sidvall.Data.CopySettings copySettings, string parent, string child);
        partial void OnAfterImport(System.Collections.Generic.IEnumerable<IWorkItemType> destination, System.Collections.Generic.IEnumerable<IWorkItemType> source, Sidvall.Data.CopySettings copySettings, string parent, string child);
        internal void Import(System.Collections.Generic.IEnumerable<IWorkItemType> destination, System.Collections.Generic.IEnumerable<IWorkItemType> source, Sidvall.Data.CopySettings copySettings, string parent, string child)
        {
            OnBeforeImport(destination, source, copySettings, parent, child);
            ImportItems(destination, source, copySettings, parent, child);
            OnAfterImport(destination, source, copySettings, parent, child);
        }

        #endregion
        #region DataRowIsValid

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]
        public bool DataRowIsValid(IWorkItemType value, Sidvall.Data.BusinessRuleFilter filter)
        {
            if (value == null)
                return true;
            if (filter == null)
                filter = Sidvall.Data.BusinessRuleFilter.CreateFilter();
            var provider = value as Sidvall.Data.IValidParentProvider;
            if ((provider != null) && (!provider.IsSelfValid(filter)))
                return false;
            if (!this.EntityFacadeManager.GenericEntityFacade.WorkItemManager.DataRowIsValid(value.WorkItems, filter))
                return false;
            return true;
        }
        public bool DataRowIsValid(System.Collections.Generic.IEnumerable<IWorkItemType> value, Sidvall.Data.BusinessRuleFilter filter)
        {
            if (value == null)
                return true;
            foreach (var item in value)
            {
                if (!DataRowIsValid(item, filter))
                    return false;
            }
            return true;
        }

        #endregion
        #region DataRowStateIsDirty

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]
        public bool DataRowStateIsDirty(IWorkItemType value)
        {
            if (value == null)
                return false;
            if (value.DataRowStateIsSelfDirty)
                return true;
            if (this.EntityFacadeManager.GenericEntityFacade.WorkItemManager.DataRowStateIsDirty(value.WorkItems))
                return true;
            return false;
        }
        public bool DataRowStateIsDirty(System.Collections.Generic.IEnumerable<IWorkItemType> value)
        {
            return IsDirtyItems(value);
        }

        #endregion
        #region ValidateAsync

        public async System.Threading.Tasks.Task<Sidvall.Data.BusinessRuleCollection> ValidateAsync(IWorkItemType value)
        {
            return await ValidateAsync(value, (Sidvall.Data.ValidationSettings)null).ConfigureAwait(false);
        }
        public async System.Threading.Tasks.Task<Sidvall.Data.BusinessRuleCollection> ValidateAsync(IWorkItemType value, Sidvall.Data.ValidationSettings settings)
        {
            var context = new Sidvall.Business.ValidationContext(settings);
            await ValidateAsync(value, context).ConfigureAwait(false);
            return context.AllBusinessRules;
        }
        partial void OnBeforeValidate(IWorkItemType value, Sidvall.Data.BusinessRuleCollection itemBusinessRules, Sidvall.Business.ValidationContext context);
        partial void OnAfterValidate(IWorkItemType value, Sidvall.Data.BusinessRuleCollection itemBusinessRules, Sidvall.Business.ValidationContext context);
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling")]
        public async System.Threading.Tasks.Task ValidateAsync(IWorkItemType value, Sidvall.Business.ValidationContext context)
        {
            if (value == null)
                return;
            var itemBusinessRules = new Sidvall.Data.BusinessRuleCollection();
            OnBeforeValidate(value, itemBusinessRules, context);
            await context.TaskContext.DoTaskAsync().ConfigureAwait(false);
            if (context.Settings.IncludeChildren)
            {
                await this.EntityFacadeManager.GenericEntityFacade.WorkItemManager.ValidateAsync(value.WorkItems, context).ConfigureAwait(false);
            }
            OnAfterValidate(value, itemBusinessRules, context);
            await context.TaskContext.DoTaskAsync().ConfigureAwait(false);
            context.AllBusinessRules.AddRange(context.Settings.IncludeNotBrokenRules, itemBusinessRules);
            var businessRulesProvider = value as Sidvall.Data.IBusinessRulesProvider;
            if (businessRulesProvider != null)
                businessRulesProvider.BusinessRules.Merge(context.Settings.IncludeNotBrokenRules, itemBusinessRules);
        }
        public async System.Threading.Tasks.Task<Sidvall.Data.BusinessRuleCollection> ValidateAsync(System.Collections.Generic.IEnumerable<IWorkItemType> value)
        {
            return await ValidateAsync(value, (Sidvall.Data.ValidationSettings)null).ConfigureAwait(false);
        }
        public async System.Threading.Tasks.Task<Sidvall.Data.BusinessRuleCollection> ValidateAsync(System.Collections.Generic.IEnumerable<IWorkItemType> value, Sidvall.Data.ValidationSettings settings)
        {
            var context = new Sidvall.Business.ValidationContext(settings);
            await ValidateAsync(value, context).ConfigureAwait(false);
            return context.AllBusinessRules;
        }
        public async System.Threading.Tasks.Task ValidateAsync(System.Collections.Generic.IEnumerable<IWorkItemType> value, Sidvall.Business.ValidationContext context)
        {
            if (value == null)
                return;
            foreach (var item in value)
                await ValidateAsync(item, context).ConfigureAwait(false);
        }

        #endregion
        #region Initialize

        public void Initialize(IWorkItemType value)
        {
            Initialize(value, null);
        }
        partial void OnAfterInitialize(IWorkItemType value);
        partial void OnBeforeInitialize(IWorkItemType value);
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        public void Initialize(IWorkItemType value, Sidvall.Data.InitializeSettings settings)
        {
            if (value == null)
                return;
            if (settings == null)
                settings = new Sidvall.Data.InitializeSettings();
            OnBeforeInitialize(value);
            var accessLevelProvider = value as Sidvall.Security.IPropertyAccessLevelProvider;
            switch (settings.FieldInitializeOption)
            {
                case Sidvall.Data.FieldInitializeOption.Reset:
                    if ((settings.FieldCopyOption & Sidvall.Business.FieldCopyOptions.AutomaticPrimaryKeys) == Sidvall.Business.FieldCopyOptions.AutomaticPrimaryKeys)
                    {
                        if (settings.IncludedProperty(accessLevelProvider, true, true, false, Sidvall.Data.DataType.Int64, "WorkItemType.WorkItemTypeId", "WorkItemType"))
                            value.WorkItemTypeId = null;
                    }
                    if ((settings.FieldCopyOption & Sidvall.Business.FieldCopyOptions.ManualNonPrimaryKeys) == Sidvall.Business.FieldCopyOptions.ManualNonPrimaryKeys)
                    {
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.String, "WorkItemType.Codename", "WorkItemType"))
                            value.Codename = string.Empty;
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.String, "WorkItemType.ContextEditor", "WorkItemType"))
                            value.ContextEditor = string.Empty;
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.String, "WorkItemType.Description", "WorkItemType"))
                            value.Description = string.Empty;
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.String, "WorkItemType.ExecutionContextEditor", "WorkItemType"))
                            value.ExecutionContextEditor = string.Empty;
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.String, "WorkItemType.Name", "WorkItemType"))
                            value.Name = string.Empty;
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Int32, "WorkItemType.ServerConnectionId", "WorkItemType"))
                            value.ServerConnectionId = null;
                    }
                    break;
                case Sidvall.Data.FieldInitializeOption.Validate:
                    if ((settings.FieldCopyOption & Sidvall.Business.FieldCopyOptions.AutomaticPrimaryKeys) == Sidvall.Business.FieldCopyOptions.AutomaticPrimaryKeys)
                    {
                        if (settings.IncludedProperty(accessLevelProvider, true, true, false, Sidvall.Data.DataType.Int64, "WorkItemType.WorkItemTypeId", "WorkItemType"))
                            value.WorkItemTypeId = Sidvall.Business.ValidationManager.GetValidInt64(value.WorkItemTypeId, null, null);
                    }
                    if ((settings.FieldCopyOption & Sidvall.Business.FieldCopyOptions.ManualNonPrimaryKeys) == Sidvall.Business.FieldCopyOptions.ManualNonPrimaryKeys)
                    {
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.String, "WorkItemType.Codename", "WorkItemType"))
                            value.Codename = Sidvall.Business.ValidationManager.GetValidString(value.Codename, false, 2000, true);
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.String, "WorkItemType.ContextEditor", "WorkItemType"))
                            value.ContextEditor = Sidvall.Business.ValidationManager.GetValidString(value.ContextEditor, false, 2000, true);
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.String, "WorkItemType.Description", "WorkItemType"))
                            value.Description = Sidvall.Business.ValidationManager.GetValidString(value.Description, false, null, true);
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.String, "WorkItemType.ExecutionContextEditor", "WorkItemType"))
                            value.ExecutionContextEditor = Sidvall.Business.ValidationManager.GetValidString(value.ExecutionContextEditor, false, 2000, true);
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.String, "WorkItemType.Name", "WorkItemType"))
                            value.Name = Sidvall.Business.ValidationManager.GetValidString(value.Name, false, 255, true);
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Int32, "WorkItemType.ServerConnectionId", "WorkItemType"))
                            value.ServerConnectionId = Sidvall.Business.ValidationManager.GetValidInt32(value.ServerConnectionId, null, null);
                    }
                    break;
                default:
                    break;
            }
            if (settings.ClearBusinessRules)
            {
                var businessRulesProvider = value as Sidvall.Data.IBusinessRulesProvider;
                if (businessRulesProvider != null)
                    businessRulesProvider.BusinessRules.Clear();
            }
            switch (settings.ChildInitializeOption)
            {
                case Sidvall.Data.RelationFieldInitializeOption.Include:
                    if (settings.IncludedProperty(accessLevelProvider, false, false, true, Sidvall.Data.DataType.Object, "WorkItemType.WorkItems", "WorkItem"))
                        this.EntityFacadeManager.GenericEntityFacade.WorkItemManager.Initialize(value.WorkItems, settings);
                    break;
                case Sidvall.Data.RelationFieldInitializeOption.Remove:
                    if (settings.IncludedProperty(accessLevelProvider, false, false, true, Sidvall.Data.DataType.Object, "WorkItemType.WorkItems", "WorkItem"))
                        value.SetWorkItems(null);
                    break;
                default:
                    break;
            }
            OnAfterInitialize(value);
        }
        public void Initialize(System.Collections.Generic.IEnumerable<IWorkItemType> value)
        {
            Initialize(value, null);
        }
        public void Initialize(System.Collections.Generic.IEnumerable<IWorkItemType> value, Sidvall.Data.InitializeSettings settings)
        {
            InitializeItems(value, settings);
        }

        #endregion
        #region EntityManagerBase Members

        protected override Sidvall.Data.IDataRowContextProvider CopyItem(Sidvall.Data.IDataRowContextProvider destination, Sidvall.Data.CopySettings copySettings, string parent, string child)
        {
            return Copy(destination as IWorkItemType, copySettings, parent, child);
        }
        protected override void ImportItem(Sidvall.Data.IDataRowContextProvider destination, Sidvall.Data.IDataRowContextProvider source, Sidvall.Data.CopySettings copySettings, string parent, string child)
        {
            Import((TEntity)destination, source as IWorkItemType, copySettings, parent, child);
        }
        protected override void AcceptItemChanges(Sidvall.Data.IDataRowContextProvider item, Sidvall.Data.AcceptChangesSettings acceptChangesSettings)
        {
            AcceptChanges(item as IWorkItemType, acceptChangesSettings);
        }
        protected override bool IsDirtyItem(Sidvall.Data.IDataRowContextProvider item)
        {
            return DataRowStateIsDirty(item as IWorkItemType);
        }
        protected override void InitializeItem(Sidvall.Data.IDataRowContextProvider item, Sidvall.Data.InitializeSettings settings)
        {
            Initialize(item as IWorkItemType, settings);
        }

        #endregion

        #endregion
        #region Private Members

        private global::Sidvall.WorkItemManager.Data.Model.IEntityFacadeManager EntityFacadeManager { get; set; }

        #endregion
        #region Constructors

        public WorkItemTypeManager(global::Sidvall.WorkItemManager.Data.Model.IEntityFacadeManager entityFacadeManager)
        {
            this.EntityFacadeManager = entityFacadeManager;
        }

        #endregion
    }
}